﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using DevM.Authorization;
using DevM.Controllers;
using DevM.Users;
using DevM.Web.Models.Users;
using DevM.Users.Dto;
using DevM.Development.Projects.Dto;
using DevM.Development.ReleaseRecords;
using DevM.Development.ReleaseRecords.Dto;
using DevM.Web.Models.ReleaseRecord;
using DevM.Development.Projects;
using DevM.Development.DataMigration;
using DevM.Development.DataMigration.Dto;
using DevM.Web.Models.DataMigration;

namespace DevM.Web.Controllers
{
    [AbpMvcAuthorize(PermissionNames.Pages_Projects)]
    public class DataMigrationController : DevMControllerBase
    {
        private readonly IReleaseRecordAppService releaseRecordAppService;
        private readonly IUserAppService userAppService;
        private readonly IDataMigrationAppService dataMigrationAppService;
        private readonly IProjectAppService projectAppService;

        public DataMigrationController(IReleaseRecordAppService releaseRecordAppService, IUserAppService userAppService, IDataMigrationAppService dataMigrationAppService, IProjectAppService projectAppService)
        {
            this.releaseRecordAppService = releaseRecordAppService;
            this.userAppService = userAppService;
            this.dataMigrationAppService = dataMigrationAppService;
            this.projectAppService = projectAppService;
        }


        public async Task<ActionResult> Configs(GetConfigListInput input)
        {
            var releaseRecord = await releaseRecordAppService.GetReleaseRecord(new EntityDto<long>(input.ReleaseRecordId));
            var project = await projectAppService.GetProject(new EntityDto<long>(releaseRecord.ProjectId));
            var configs =await dataMigrationAppService.GetConfigList(input);
            var model = new DataMigrationListViewModel<ConfigDto>
            {
                DataItems = configs,
                ReleaseRecord= releaseRecord,
                Project = project
            };
            return View(model);
        }

        public async Task<ActionResult> EditConfigModal(long configId)
        {
            var config = await dataMigrationAppService.GetConfig(new EntityDto<long>(configId));
            var model = new EditDataMigrationViewModel<ConfigDto>
            {
                Details= config
            };
            return View("_EditConfigModal", model);
        }

        public async Task<ActionResult> Datas(GetDataListInput input)
        {
            var releaseRecord = await releaseRecordAppService.GetReleaseRecord(new EntityDto<long>(input.ReleaseRecordId));
            var project = await projectAppService.GetProject(new EntityDto<long>(releaseRecord.ProjectId));
            var datas = await dataMigrationAppService.GetDataList(input);
            var model = new DataMigrationListViewModel<DataDto>
            {
                DataItems = datas,
                ReleaseRecord = releaseRecord,
                Project = project
            };
            return View(model);
        }

        public async Task<ActionResult> EditDataModal(long dataId)
        {
            var data = await dataMigrationAppService.GetData(new EntityDto<long>(dataId));
            var model = new EditDataMigrationViewModel<DataDto>
            {
                Details = data
            };
            return View("_EditDataModal", model);
        }


        public async Task<ActionResult> Databases(GetDatabaseListInput input)
        {
            var releaseRecord = await releaseRecordAppService.GetReleaseRecord(new EntityDto<long>(input.ReleaseRecordId));
            var project = await projectAppService.GetProject(new EntityDto<long>(releaseRecord.ProjectId));
            var databases = await dataMigrationAppService.GetDatabaseList(input);
            var model = new DataMigrationListViewModel<DatabaseDto>
            {
                DataItems = databases,
                ReleaseRecord = releaseRecord,
                Project = project
            };
            return View(model);
        }

        public async Task<ActionResult> EditDatabaseModal(long databaseId)
        {
            var database = await dataMigrationAppService.GetDatabase(new EntityDto<long>(databaseId));
            var model = new EditDataMigrationViewModel<DatabaseDto>
            {
                Details = database
            };
            return View("_EditDatabaseModal", model);
        }

    }
}
